#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include "../vector.h"

// 测试基本操作
void testBasicOperations() {
    printf("=== 基本功能测试 ===\n");
    
    // 测试int类型vector
    printf("\n测试int类型vector:\n");
    Vector* int_vector = VectorNew(VECTOR_TYPE_INT, 5);
    printf("创建空vector，大小: %zu\n", VectorSize(int_vector));
    
    // 添加元素
    printf("添加5个元素...\n");
    VectorAdd(int_vector, 10);
    VectorAdd(int_vector, 20);
    VectorAdd(int_vector, 30);
    VectorAdd(int_vector, 40);
    VectorAdd(int_vector, 50);
    printf("添加后，大小: %zu, 容量: %zu\n", 
           VectorSize(int_vector), VectorCapacity(int_vector));
    
    // 打印内容
    printf("vector内容: ");
    for (size_t i = 0; i < VectorSize(int_vector); i++) {
        printf("%d ", *((int*)VectorGet(int_vector, i)));
    }
    printf("\n");
    
    // 获取元素
    printf("获取元素: 索引0=%d, 索引2=%d, 索引-1=%d\n", 
           *((int*)VectorGet(int_vector, 0)), 
           *((int*)VectorGet(int_vector, 2)),
           *((int*)VectorGet(int_vector, -1)));
    
    // 设置元素
    printf("设置索引2为99\n");
    VectorSet(int_vector, 2, 99);
    printf("修改后，索引2=%d\n", *((int*)VectorGet(int_vector, 2)));
    
    // 清理
    VectorFree(int_vector);
    
    // 测试double类型vector
    printf("\n测试double类型vector:\n");
    Vector* double_vector = VectorNew(VECTOR_TYPE_DOUBLE, 3);
    VectorAdd(double_vector, 1.1);
    VectorAdd(double_vector, 2.2);
    VectorAdd(double_vector, 3.3);
    VectorPrint(double_vector);
    VectorFree(double_vector);
    
    // 测试char类型vector
    printf("\n测试char类型vector:\n");
    Vector* char_vector = VectorNew(VECTOR_TYPE_CHAR, 4);
    VectorAdd(char_vector, 'A');
    VectorAdd(char_vector, 'B');
    VectorAdd(char_vector, 'C');
    VectorAdd(char_vector, 'D');
    VectorPrint(char_vector);
    VectorFree(char_vector);
    
    printf("\n=== 基本功能测试完成 ===\n\n");
}

// 测试删除操作
void testDeleteOperations() {
    printf("=== 删除功能测试 ===\n");
    
    // 创建int类型vector
    Vector* vector = VectorNew(VECTOR_TYPE_INT, 10);
    for (int i = 10; i <= 50; i += 10) {
        VectorAdd(vector, i);
    }
    
    printf("原始vector: ");
    VectorPrint(vector);
    
    // 删除单个元素
    printf("删除索引2的元素...\n");
    VectorDelete(vector, 2);
    printf("删除后: ");
    VectorPrint(vector);
    
    // 删除负索引元素
    printf("删除索引-1的元素...\n");
    VectorDelete(vector, -1);
    printf("删除后: ");
    VectorPrint(vector);
    
    // 删除范围元素
    printf("删除索引0-1的元素...\n");
    VectorDeleteRange(vector, 0, 2);
    printf("删除后: ");
    VectorPrint(vector);
    
    // 清空vector
    printf("清空vector...\n");
    VectorClear(vector);
    printf("清空后，大小: %zu\n", VectorSize(vector));
    
    VectorFree(vector);
    printf("\n=== 删除功能测试完成 ===\n\n");
}

// 测试查找操作
void testSearchOperations() {
    printf("=== 查找功能测试 ===\n");
    
    // 创建int类型vector
    Vector* vector = VectorNew(VECTOR_TYPE_INT, 10);
    int values[] = {10, 20, 30, 20, 40, 20};
    for (size_t i = 0; i < sizeof(values)/sizeof(values[0]); i++) {
        VectorAdd(vector, values[i]);
    }
    
    printf("vector内容: ");
    VectorPrint(vector);
    
    // 测试VectorHas
    int search_value = 20;
    printf("值 %d 是否存在: %s\n", 
           search_value, VectorHas(vector, &search_value) ? "是" : "否");
    
    // 测试VectorIndex
    int index = VectorIndex(vector, &search_value);
    printf("值 %d 的第一个索引: %d\n", search_value, index);
    
    // 测试VectorCountValue
    size_t count = VectorCountValue(vector, &search_value);
    printf("值 %d 出现次数: %zu\n", search_value, count);
    
    // 测试VectorRemove
    printf("删除第一个值 %d...\n", search_value);
    int removed = VectorRemove(vector, &search_value);
    printf("删除结果: %s\n", removed ? "成功" : "失败");
    printf("删除后: ");
    VectorPrint(vector);
    
    VectorFree(vector);
    printf("\n=== 查找功能测试完成 ===\n\n");
}

// 测试扩展和复制
void testExtendAndCopy() {
    printf("=== 扩展和复制测试 ===\n");
    
    // 创建两个vector
    Vector* vector1 = VectorNew(VECTOR_TYPE_FLOAT, 5);
    VectorAdd(vector1, 1.1f);
    VectorAdd(vector1, 2.2f);
    VectorAdd(vector1, 3.3f);
    
    Vector* vector2 = VectorNew(VECTOR_TYPE_FLOAT, 5);
    VectorAdd(vector2, 4.4f);
    VectorAdd(vector2, 5.5f);
    VectorAdd(vector2, 6.6f);
    
    printf("vector1: ");
    VectorPrint(vector1);
    printf("vector2: ");
    VectorPrint(vector2);
    
    // 扩展vector1
    printf("扩展vector1...\n");
    VectorExtend(vector1, vector2);
    printf("扩展后vector1: ");
    VectorPrint(vector1);
    
    // 复制vector
    printf("复制vector1...\n");
    Vector* copied = VectorCopy(vector1);
    printf("复制的vector: ");
    VectorPrint(copied);
    
    // 清理
    VectorFree(vector1);
    VectorFree(vector2);
    VectorFree(copied);
    printf("\n=== 扩展和复制测试完成 ===\n\n");
}

// 测试从数组创建
void testFromArray() {
    printf("=== 从数组创建测试 ===\n");
    
    // 从int数组创建
    int int_array[] = {100, 200, 300, 400};
    Vector* int_vector = VectorFromArray(VECTOR_TYPE_INT, int_array, sizeof(int_array)/sizeof(int_array[0]));
    printf("从int数组创建: ");
    VectorPrint(int_vector);
    
    // 从double数组创建
    double double_array[] = {1.1, 2.2, 3.3};
    Vector* double_vector = VectorFromArray(VECTOR_TYPE_DOUBLE, double_array, sizeof(double_array)/sizeof(double_array[0]));
    printf("从double数组创建: ");
    VectorPrint(double_vector);
    
    // 清理
    VectorFree(int_vector);
    VectorFree(double_vector);
    printf("\n=== 从数组创建测试完成 ===\n\n");
}

// 测试排序和反转
void testSortAndReverse() {
    printf("=== 排序和反转测试 ===\n");
    
    // 创建int类型vector
    Vector* vector = VectorNew(VECTOR_TYPE_INT, 10);
    int values[] = {50, 20, 40, 10, 30};
    for (size_t i = 0; i < sizeof(values)/sizeof(values[0]); i++) {
        VectorAdd(vector, values[i]);
    }
    
    printf("原始vector: ");
    VectorPrint(vector);
    
    // 测试排序
    printf("排序vector...\n");
    VectorSort(vector);
    printf("排序后: ");
    VectorPrint(vector);
    
    // 测试反转
    printf("反转vector...\n");
    VectorReverse(vector);
    printf("反转后: ");
    VectorPrint(vector);
    
    // 获取最小最大值
    int* min_val = (int*)VectorMinValue(vector);
    int* max_val = (int*)VectorMaxValue(vector);
    printf("最小值: %d, 最大值: %d\n", *min_val, *max_val);
    
    VectorFree(vector);
    printf("\n=== 排序和反转测试完成 ===\n\n");
}

// 测试切片操作
void testSlice() {
    printf("=== 切片操作测试 ===\n");
    
    // 创建int类型vector
    Vector* vector = VectorNew(VECTOR_TYPE_INT, 10);
    for (int i = 1; i <= 10; i++) {
        VectorAdd(vector, i * 10);
    }
    
    printf("原始vector: ");
    VectorPrint(vector);
    
    // 测试切片
    Vector* slice1 = VectorSlice(vector, 2, 6, 1);
    printf("切片[2:6]: ");
    VectorPrint(slice1);
    
    Vector* slice2 = VectorSlice(vector, 0, 10, 2);
    printf("切片[0:10:2]: ");
    VectorPrint(slice2);
    
    Vector* slice3 = VectorSlice(vector, -5, -1, 1);
    printf("切片[-5:-1]: ");
    VectorPrint(slice3);
    
    // 清理
    VectorFree(vector);
    VectorFree(slice1);
    VectorFree(slice2);
    VectorFree(slice3);
    printf("\n=== 切片操作测试完成 ===\n\n");
}

// 测试错误处理
void testErrorHandling() {
    printf("=== 错误处理测试 ===\n");
    
    // 测试NULL vector
    printf("测试NULL vector:\n");
    VectorFree(NULL);
    printf("VectorSize(NULL) = %zu\n", VectorSize(NULL));
    
    // 创建有效vector
    Vector* vector = VectorNew(VECTOR_TYPE_INT, 5);
    VectorAdd(vector, 10);
    VectorAdd(vector, 20);
    
    // 测试索引越界
    printf("\n测试索引越界:\n");
    void* val_ptr = VectorGet(vector, 10);  // 越界索引
    printf("VectorGet(vector, 10) = %p\n", val_ptr);
    
    // 测试类型不匹配
    printf("\n测试类型不匹配:\n");
    Vector* float_vector = VectorNew(VECTOR_TYPE_FLOAT, 5);
    VectorExtend(vector, float_vector);  // 类型不匹配
    
    // 测试切片步长为0
    printf("\n测试切片步长为0:\n");
    Vector* invalid_slice = VectorSlice(vector, 0, 5, 0);
    printf("VectorSlice步长为0返回: %p\n", invalid_slice);
    
    // 清理
    VectorFree(vector);
    VectorFree(float_vector);
    printf("\n=== 错误处理测试完成 ===\n\n");
}

int main() {
    // 设置区域为中文UTF-8
    setlocale(LC_ALL, "zh_CN.UTF-8");
    printf("开始测试Vector库...\n\n");
    
    // 运行各种测试
    testBasicOperations();
    testDeleteOperations();
    testSearchOperations();
    testExtendAndCopy();
    testFromArray();
    testSortAndReverse();
    testSlice();
    testErrorHandling();
    
    printf("所有测试完成！\n");
    return 0;
}